package org.han.login.controller;

import org.han.annotation.RonghuanetPermission;
import org.han.login.constant.LoginConstant;
import org.han.login.service.ILoginService;
import org.han.org.dto.LoginDTO;
import org.han.util.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RonghuanetPermission(name = "登陆")
public class LoginController {
    @Resource
    private ILoginService loginService;


    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO dto){
        try {
            Map<String, Object> login = loginService.login(dto);
            return AjaxResult.me().setData(login);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登陆失败!!!" + e.getMessage());
        }
    }

    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        try {
            // 获取token
            String token = request.getHeader("token");
            LoginConstant.loginMap.remove(token);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("退出失败!!!" + e.getMessage());
        }
    }

}
